package com.sayonaragutgut.android.activities;

import java.util.List;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sayonaragutgut.android.R;
import com.sayonaragutgut.android.core.VagasDataService;
import com.sayonaragutgut.android.core.VagasEntry;
import com.sayonaragutgut.android.core.VagasEntryAdapter;

public class ListaVagasActivity extends Activity {
	private List<VagasEntry> entries;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_vagas);
		
		final ListView vagasListView = (ListView) findViewById(R.id.vagasListView);
		final VagasEntryAdapter adapter = new VagasEntryAdapter(this, R.layout.vaga_entry_list_item);
		vagasListView.setAdapter(adapter);
		
		vagasListView.setOnItemClickListener(itemClickListener);
		
		entries = getVagasEntries();
		for (final VagasEntry entry : entries) {
			adapter.add(entry);
		}
	}
	
	private final AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			initializeDetalheVagaActivity(position);
		}
	};
	
	private void initializeDetalheVagaActivity(int position) {
		Intent detalheVagaIntent = new Intent(this, DetalheVagaActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("VagaPosition", position);
		detalheVagaIntent.putExtras(bundle);
		startActivity(detalheVagaIntent);
	}
	
	private List<VagasEntry> getVagasEntries() {
		return (new VagasDataService()).getVagasEntries();
	}
}
